<?php

namespace App\Http\Controllers;

use App\Http\Requests\SwitchChapterRequest;
use App\Models\Chapter;
use App\Models\Repository;
use Illuminate\Http\Request;

class RepositorySequenceController extends Controller
{
    public function index(Repository $repository, $sequence)
    {

        $grouped_chapters = $repository->sequence($sequence)->get()->groupBy('pivot.is_using')->sortKeysDesc();

        return view('resources.repository.sequence.index')->with(compact('repository', 'grouped_chapters', 'sequence'));

    }

    public function switchChapter(SwitchChapterRequest $request, Repository $repository, Chapter $chapter,  $sequence)
    {

        $chapter = $chapter->findOrFail($request->id);


        $repository->switchChapter($chapter, $sequence);

        return back();
    }
}
